package com.steven.im.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.google.zxing.Result;
import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import com.steven.im.R;
import com.steven.im.base.Constant;
import com.steven.im.utils.ImageScanningTask;
import com.steven.im.utils.StatusBarUtil;
import com.steven.im.utils.TitleBuilder;

import io.reactivex.annotations.NonNull;

/**
 * 扫描二维码
 */

public class IMScanActivity extends FragmentActivity {
    private CaptureManager capture;
    private DecoratedBarcodeView barcodeScannerView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //禁止横竖屏切换
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //设置状态栏全透明
        StatusBarUtil.setStatusBar(this, true);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        barcodeScannerView = initializeContent();
        new TitleBuilder(this).setTitleText(getString(R.string.scan))
                .setRightText(getString(R.string.xiangce))
                .setLeftIcoListening(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finish();
                    }
                });
        /*根据闪光灯状态设置imagebutton*/
        barcodeScannerView.setTorchListener(new DecoratedBarcodeView.TorchListener() {
            @Override
            public void onTorchOn() {
            }

            @Override
            public void onTorchOff() {
            }
        });
        capture = new CaptureManager(this, barcodeScannerView);

        capture.initializeFromIntent(getIntent(), savedInstanceState);

        capture.decode();
    }

    /**
     * Override to use a different layout.
     *
     * @return the DecoratedBarcodeView
     */
    protected DecoratedBarcodeView initializeContent() {
        setContentView(R.layout.activity_im_scan);
        return (DecoratedBarcodeView) findViewById(R.id.dbv);
    }

    @Override
    protected void onResume() {
        super.onResume();
        capture.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        capture.onPause();
        barcodeScannerView.setTorchOff();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        capture.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        capture.onSaveInstanceState(outState);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
        capture.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return barcodeScannerView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
    }

    public void pickImage(View view) {
        Constant.isPhoto = false;
        Intent intent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, Constant.PhotoCode);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Constant.PhotoCode) {
            if (Activity.RESULT_OK == resultCode && null != data) {
                Uri uri = data.getData();
                ImageScanningTask scanningTask = new ImageScanningTask(uri,
                        new ImageScanningTask.ImageScanningCallback() {
                            @Override
                            public void onFinishScanning(Result result) {
                                if (result != null) {
                                    Constant.isPhoto = true;
                                    //表示识别成功
                                    Intent intent = new Intent();
                                    intent.setClass(IMScanActivity.this, IMUserInfoActivity.class);
                                    intent.putExtra("result", result.getText());
                                    startActivity(intent);
                                    finish();
                                } else {
                                    Constant.isPhoto = false;
                                    // 图册二维码识别失败
                                    Toast.makeText(IMScanActivity.this, "Scanned error!", Toast.LENGTH_LONG).show();
                                }
                            }
                        });
                scanningTask.execute();
            }
        }
    }
}
